package com.hqyj.controller;


import com.hqyj.entity.QiNiuUtils;
import com.hqyj.entity.SearchBean;
import com.hqyj.pojo.TbOwner;
import com.hqyj.service.TbOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.util.HashMap;
import java.util.UUID;

/**
 * <p>
 * 业主表 前端控制器
 * </p>
 *
 * @author gp
 * @since 2021-09-26
 */
@RestController
@RequestMapping("/tb-owner")
public class TbOwnerController {

    @Autowired
    private TbOwnerService tbOwnerService;

    @GetMapping("/list")
    public HashMap<String,Object> tOList(SearchBean searchBean){
      return tbOwnerService.tOList(searchBean);
    }

    @GetMapping("/delete")
    public HashMap<String,Object> delete(int id){
        return tbOwnerService.deleteTO(id);
    }

    @PostMapping("/update")
    public HashMap<String,Object> update(@RequestBody TbOwner tbOwner){
        return tbOwnerService.updateTO(tbOwner);
    }

    @PostMapping("/insert")
    public HashMap<String,Object> insert(@RequestBody TbOwner tbOwner){
        return tbOwnerService.insertTO(tbOwner);
    }

    @GetMapping("/findById")
    public HashMap<String,Object> findById(int id){
        return tbOwnerService.findById(id);
    }

    @PostMapping("/upload")
    public HashMap<String, String> upload(MultipartFile imgFile){
        HashMap<String,String> map = new HashMap<String,String>();
        // -获取原有图片名称，截取到后缀名
        String originalFilename = imgFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //  -生成唯一文件名，拼接后缀名
        String filename = UUID.randomUUID() + extension;
        // 调用七牛云上传文件
        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),filename);
            map.put("info","上传成功");
            map.put("imgName",filename);
            map.put("domain",QiNiuUtils.DOMAIN);

        } catch (IOException e) {
            e.printStackTrace();
            map.put("info","上传失败");
        }
        //返回数据给页面
        return map;

    }


}

